package com.iyao_app.adapter;

import android.content.Context;
import android.net.Uri;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;

import com.bumptech.glide.Glide;
import com.chad.library.adapter.base.BaseQuickAdapter;
import com.facebook.drawee.backends.pipeline.Fresco;
import com.facebook.drawee.backends.pipeline.PipelineDraweeController;
import com.facebook.drawee.view.SimpleDraweeView;
import com.facebook.imagepipeline.common.ResizeOptions;
import com.facebook.imagepipeline.request.ImageRequest;
import com.facebook.imagepipeline.request.ImageRequestBuilder;
import com.iyao_app.MyApplication;
import com.iyao_app.R;
import com.iyao_app.util.ResourcesUtils;
import com.iyao_app.view.OnImageClickListener;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;

/**
 * Created by tongxue on 2017/11/30.
 */

public class PicturesAdapter extends RecyclerView.Adapter<PicturesAdapter.FindAttention> {

    @BindView(R.id.rv_horizontal)
    RecyclerView rvHorizontal;
    @BindView(R.id.image_pictures)
    ImageView imagePictures;

    private RecyclerView recyclerView;
    public List<String> list = new ArrayList<>();
    private LayoutInflater inflater;
    private Context context;
    private OnImageClickListener findonclick;

    public PicturesAdapter(RecyclerView recyclerView, List<String> list, OnImageClickListener findonclick) {
        this.recyclerView = recyclerView;
        this.list = list;
        this.findonclick = findonclick;
    }


    @Override
    public FindAttention onCreateViewHolder(ViewGroup parent, int viewType) {
        View view = null;
        context = parent.getContext();
        inflater = LayoutInflater.from(context);
        if (list.size() == 1) {
            view = inflater.inflate(R.layout.item_picturesss_one, null);
        } else {
            view = inflater.inflate(R.layout.item_picturesss, null);
        }

        return new FindAttention(view);
    }

    @Override
    public void onBindViewHolder(FindAttention holder, final int position) {
        if (position > 8) {

        } else {

            try {
                Uri uri = Uri.parse(list.get(position));
                ImageRequest request = ImageRequestBuilder.newBuilderWithSource(uri)
                        .setResizeOptions(new ResizeOptions((int) ResourcesUtils.getDimension(R.dimen.x360), (int) ResourcesUtils.getDimension(R.dimen.x360)))
                        .build();
                PipelineDraweeController controller = (PipelineDraweeController) Fresco.newDraweeControllerBuilder()
                        .setOldController(holder.imagePictures.getController())
                        .setImageRequest(request)
                        .build();
                holder.imagePictures.setController(controller);
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
        holder.itemView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if (findonclick != null) {
                    findonclick.onImageClick(list, position);
                }
            }
        });
    }

    @Override
    public int getItemCount() {
        if (list != null) {
            if (list.size() > 9) {
                return 9;
            }
            return list.size();
        }
        return 0;
    }

    class FindAttention extends RecyclerView.ViewHolder {


        @BindView(R.id.image_pictures)
        SimpleDraweeView imagePictures;

        public FindAttention(View itemView) {
            super(itemView);
            ButterKnife.bind(this, itemView);

        }
    }
}
